Imports System.Data.OracleClient

Public Class clsGnrtStruct

  Private clsobjFileGnrt As clsGnrtFile

  Private mCmdData As OracleCommand
  Private mDrData As OracleDataReader
  Private mCmdDataVar As OracleCommand
  Private mDrDataVar As OracleDataReader
  Private mCmdVarGetSetPrpty As OracleCommand
  Private mDrDataVarGetSet As OracleDataReader

  Public mIntMainClassId As Integer
  Private mStrMainClassName As String
  Private sStrCode As String
  Private sMclsCatg As String = ""





  Public Sub GenerateStructure()

    clsobjFileGnrt = New clsGnrtFile

    sStrCode = sStrCode + "Imports System" & vbCrLf & _
                          "Imports System.IO" & vbCrLf & _
                          "Imports System.Collections" & vbCrLf & _
                          "Imports System.Runtime.InteropServices" & vbCrLf & vbCrLf

    If mIntMainClassId <> Nothing Then

      GetMainClass()
      GetSubClass()

      sStrCode = sStrCode & gCONST_SPACES_MAIN_CLASS & "End Class"
      sStrCode = sStrCode & vbCrLf & "End Namespace"
      clsobjFileGnrt.mprGnrtVbFiles(sStrCode, "cls" & mStrMainClassName, "Structure Body")

    Else
      MessageBox.Show("Invalid data", gCONST_MSG_HEADER, MessageBoxButtons.OK)
    End If

  End Sub






  Private Sub GetMainClass()

    Dim lStrQuery As String = ""

    lStrQuery = "select MCLS_NAME,MCLS_SCOP,MCLS_CATG from TRDX_MAIN_CLASS where MCLS_IDXX = " & mIntMainClassId

    mCmdData = New OracleCommand(lStrQuery, con)
    mDrData = mCmdData.ExecuteReader()

    sStrCode = sStrCode & "Namespace CommsGlobalStructures" & vbCrLf

    While mDrData.Read

      sStrCode = sStrCode + gCONST_SPACES_MAIN_CLASS + mDrData(1).ToString & " Class cls" _
                & mDrData(0).ToString & vbCrLf & vbCrLf
      mStrMainClassName = mDrData("MCLS_NAME")

      If mDrData("MCLS_CATG") = gCONST_MCLS_CATG_FIXX Then
        sMclsCatg = gCONST_CHARSET_RTURN_FIXX
      ElseIf mDrData("MCLS_CATG") = gCONST_MCLS_CATG_OTHR Then
        sMclsCatg = gCONST_CHARSET_RTURN_OTHR
      End If

    End While

  End Sub





  Private Sub GetSubClass()

    Dim lcount As Integer = 0
    Dim lStrQuery As String = ""

    lStrQuery = "select SCLS_CLAS_NAME,SCLS_SCOP,SCLS_PACK,SCLS_CLAS_IDXX from TRDX_SUB_CLASS where "
    lStrQuery = lStrQuery & "SCLS_MAIN_CLAS_IDXX = " & mIntMainClassId & " order by SCLS_MAIN_CLAS_IDXX"

    mCmdData = New OracleCommand(lStrQuery, con)
    mDrData = mCmdData.ExecuteReader

    While mDrData.Read
      sStrCode = sStrCode & gCONST_SPACES2 & " <Serializable()> _"
      sStrCode = sStrCode & gCONST_SPACES2 & "<StructLayout(LayoutKind.Sequential, CharSet:=CharSet." & sMclsCatg
      sStrCode = sStrCode & ", Pack:= "
      sStrCode = sStrCode & mDrData(2) & ")> _" & vbCrLf
      sStrCode = sStrCode & gCONST_SPACES2 & mDrData(1) & " Class cls" & mDrData(0) & vbCrLf

      Dim lSub_Class_Id = mDrData(3)

      GetVariables(lSub_Class_Id)
      get_set_properties(lSub_Class_Id, mIntMainClassId)


      sStrCode = sStrCode & gCONST_SPACES2 & "End Class" & vbCrLf & vbCrLf & vbCrLf

    End While

  End Sub





  Private Sub GetVariables(ByVal lSclsIdxx As Integer)

    Dim var_count As Integer = 0
    Dim lStrVarQuery As String
    Dim cmd_clas_name As New OracleCommand


    lStrVarQuery = "select VRBL_IDXX,VRBL_SQNC,VRBL_NAME,VRBL_MCLS_IDXX,VRBL_SCLS_IDXX,VRBL_DATA_TYPE,VRBL_SCOP,"
    lStrVarQuery = lStrVarQuery & "VRBL_TYPE,nvl(VRBL_SIZE,'*') VRBL_SIZE,VRBL_REFR_NAME,VRBL_FIXX_NAME,"
    lStrVarQuery = lStrVarQuery & "VRBL_NULL_INDC from TRDX_VARIABLES where VRBL_MCLS_IDXX = "
    lStrVarQuery = lStrVarQuery & mIntMainClassId & " and VRBL_SCLS_IDXX = "
    lStrVarQuery = lStrVarQuery & lSclsIdxx & " order by VRBL_SQNC"

    mCmdDataVar = New OracleCommand(lStrVarQuery, con)
    mDrDataVar = mCmdDataVar.ExecuteReader

    While mDrDataVar.Read

      sStrCode = sStrCode & gCONST_SPACES4

      If mDrDataVar("VRBL_TYPE") = gCONST_NORMAL Then

        sStrCode = sStrCode & mDrDataVar("VRBL_SCOP") & gCONST_SPACES1
        sStrCode = sStrCode & fnPassVarPrefix(mDrDataVar("VRBL_DATA_TYPE"), mDrDataVar("VRBL_TYPE"))
        sStrCode = sStrCode & mDrDataVar("VRBL_NAME") & " As "
        sStrCode = sStrCode & fnGetPrptRtrnType(mDrDataVar("VRBL_DATA_TYPE")) & vbCrLf

      ElseIf mDrDataVar("VRBL_TYPE") = gCONST_ARRAY Then

        sStrCode = sStrCode & "<MarshalAs (UnmanagedType.ByValArray, SizeConst:=("
        sStrCode = sStrCode & mDrDataVar("VRBL_SIZE").ToString

        If mDrDataVar("VRBL_NULL_INDC") = "Y" Then
          sStrCode = sStrCode & " + 1"
        End If

        sStrCode = sStrCode & "))> _" & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES4 & mDrDataVar("VRBL_SCOP") & gCONST_SPACES1
        sStrCode = sStrCode & fnPassVarPrefix(mDrDataVar("VRBL_DATA_TYPE"), mDrDataVar("VRBL_TYPE"))
        sStrCode = sStrCode & mDrDataVar("VRBL_NAME") & "() As "
        sStrCode = sStrCode & fnGetPrptRtrnType(mDrDataVar("VRBL_DATA_TYPE")) & vbCrLf

      ElseIf mDrDataVar("VRBL_TYPE") = gCONST_USER_DEFINED Then

        Dim sClass_Name As String

        sClass_Name = mDrDataVar("VRBL_REFR_NAME")
        sStrCode = sStrCode & mDrDataVar("VRBL_SCOP") & gCONST_SPACES1
        sStrCode = sStrCode & fnPassVarPrefix(mDrDataVar("VRBL_DATA_TYPE"), mDrDataVar("VRBL_TYPE"))
        sStrCode = sStrCode & mDrDataVar("VRBL_NAME") & " As New cls" & sClass_Name & vbCrLf

      ElseIf mDrDataVar("VRBL_TYPE") = gCONST_USER_DEFINED_ARRAY Then

        Dim sClass_Name As String

        sClass_Name = mDrDataVar("VRBL_REFR_NAME")

        If mDrDataVar("VRBL_SIZE") <> "*" Then

          sStrCode = sStrCode & "<MarshalAs (UnmanagedType.ByValArray, SizeConst:=("
          sStrCode = sStrCode & mDrDataVar("VRBL_SIZE")

          If mDrDataVar("VRBL_NULL_INDC") = "Y" Then
            sStrCode = sStrCode & " + 1"
          End If

          sStrCode = sStrCode & "))> _" & vbCrLf
          sStrCode = sStrCode & gCONST_SPACES4

        End If

        sStrCode = sStrCode & mDrDataVar("VRBL_SCOP") & gCONST_SPACES1
        sStrCode = sStrCode & fnGetPrptPrfx(mDrDataVar("VRBL_DATA_TYPE"), mDrDataVar("VRBL_TYPE"))
        sStrCode = sStrCode & mDrDataVar("VRBL_NAME") & "() As cls" & sClass_Name & vbCrLf

      End If

    End While

  End Sub





  Private Sub get_set_properties(ByVal sSub_class_id As Integer, ByVal sMain_class_id As Integer)

    Dim lStrQueryPrpty As String = ""


    lStrQueryPrpty = "select VRBL_NAME,VRBL_DATA_TYPE,VRBL_TYPE from TRDX_VARIABLES where VRBL_MCLS_IDXX = "
    lStrQueryPrpty = lStrQueryPrpty & sMain_class_id & " and VRBL_SCLS_IDXX = "
    lStrQueryPrpty = lStrQueryPrpty & sSub_class_id & " order by VRBL_SQNC"

    mCmdVarGetSetPrpty = New OracleCommand(lStrQueryPrpty, con)
    mDrDataVarGetSet = mCmdVarGetSetPrpty.ExecuteReader

    sStrCode = sStrCode & vbCrLf

    While mDrDataVarGetSet.Read

      If mDrDataVarGetSet("VRBL_TYPE") = gCONST_USER_DEFINED Then
        'do nothing 
      ElseIf mDrDataVarGetSet("VRBL_TYPE") = gCONST_USER_DEFINED_ARRAY Then
        'do nothing
      ElseIf mDrDataVarGetSet("VRBL_TYPE") = gCONST_ARRAY Then

        sStrCode = sStrCode & gCONST_SPACES4
        sStrCode = sStrCode & "Public Property prp"
        sStrCode = sStrCode & fnGetPrptPrfx(mDrDataVarGetSet("VRBL_DATA_TYPE"), mDrDataVarGetSet("VRBL_TYPE"))
        sStrCode = sStrCode & mDrDataVarGetSet("VRBL_NAME") & "() As "
        sStrCode = sStrCode & fnGetPrptRtrnType(mDrDataVarGetSet("VRBL_DATA_TYPE")) & "()" & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & "Get" & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & gCONST_SPACES2 & "Return m"
        sStrCode = sStrCode & fnGetPrptPrfx(mDrDataVarGetSet("VRBL_DATA_TYPE"), mDrDataVarGetSet("VRBL_TYPE"))
        sStrCode = sStrCode & mDrDataVarGetSet("VRBL_NAME") & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & "End Get" & vbCrLf & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & "Set(ByVal value() As "
        sStrCode = sStrCode & fnGetPrptRtrnType(mDrDataVarGetSet("VRBL_DATA_TYPE")) & ")" & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & gCONST_SPACES2 & "m"
        sStrCode = sStrCode & fnGetPrptPrfx(mDrDataVarGetSet("VRBL_DATA_TYPE"), mDrDataVarGetSet("VRBL_TYPE"))
        sStrCode = sStrCode & mDrDataVarGetSet("VRBL_NAME") & " = value" & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & "End Set" & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES4 & "End Property" & vbCrLf & vbCrLf

      ElseIf mDrDataVarGetSet("VRBL_TYPE") = gCONST_NORMAL Then

        sStrCode = sStrCode & gCONST_SPACES4
        sStrCode = sStrCode & "Public Property prp"
        sStrCode = sStrCode & fnGetPrptPrfx(mDrDataVarGetSet("VRBL_DATA_TYPE"), mDrDataVarGetSet("VRBL_TYPE"))
        sStrCode = sStrCode & mDrDataVarGetSet("VRBL_NAME") & "() As "
        sStrCode = sStrCode & fnGetPrptRtrnType(mDrDataVarGetSet("VRBL_DATA_TYPE")) & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & "Get" & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & gCONST_SPACES2 & "Return m"
        sStrCode = sStrCode & fnGetPrptPrfx(mDrDataVarGetSet("VRBL_DATA_TYPE"), mDrDataVarGetSet("VRBL_TYPE"))
        sStrCode = sStrCode & mDrDataVarGetSet("VRBL_NAME") & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & "End Get" & vbCrLf & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & "Set(ByVal value As "
        sStrCode = sStrCode & fnGetPrptRtrnType(mDrDataVarGetSet("VRBL_DATA_TYPE")) & ")" & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & gCONST_SPACES2 & "m"
        sStrCode = sStrCode & fnGetPrptPrfx(mDrDataVarGetSet("VRBL_DATA_TYPE"), mDrDataVarGetSet("VRBL_TYPE"))
        sStrCode = sStrCode & mDrDataVarGetSet("VRBL_NAME") & " = value" & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES6 & "End Set" & vbCrLf
        sStrCode = sStrCode & gCONST_SPACES4 & "End Property" & vbCrLf & vbCrLf

      End If

    End While

  End Sub






  Public Function fnPassVarPrefix(ByVal sData_Type As String, ByVal sVar_Type As String) As String

    If sVar_Type = gCONST_ARRAY Then
      Select Case (sData_Type)
        Case ("Short")
          Return "mArrSrt"
        Case ("Int16")
          Return "mArrInt"
        Case ("Int32")
          Return "mArrInt"
        Case ("Float")
          Return "mArrFlt"
        Case ("Double")
          Return "mArrDbl"
        Case ("Long")
          Return "mArrLng"
        Case ("UShort")
          Return "mArrUsrt"
        Case ("UInt32")
          Return "mArrUint"
        Case ("Byte")
          Return "mArrByt"
        Case ("Single")
          Return "mArrSng"
        Case ("String")
          Return "mArrStr"
        Case ("Char")
          Return "mArrChr"
        Case ("User Defined DataType Array")
          Return "mArrObj"
        Case Else
          Return ""
      End Select
    Else
      Select Case (sData_Type)
        Case ("Short")
          Return "mSrt"
        Case ("Int16")
          Return "mInt"
        Case ("Int32")
          Return "mInt"
        Case ("Float")
          Return "mFlt"
        Case ("Double")
          Return "mDbl"
        Case ("Long")
          Return "mLng"
        Case ("UShort")
          Return "mUsrt"
        Case ("UInt32")
          Return "mUint"
        Case ("Byte")
          Return "mByt"
        Case ("Single")
          Return "mSng"
        Case ("String")
          Return "mStr"
        Case ("Char")
          Return "mChr"
        Case ("User Defined DataType")
          Return "mObj"
        Case ("User Defined DataType Array")
          Return "mArrObj"
        Case Else
          Return ""
      End Select
    End If

  End Function





  Private Function fnGetPrptPrfx(ByVal lData_Type As String, ByVal lVar_Type As String) As String
    Dim lStrDataType As String = ""

    If lVar_Type = gCONST_NORMAL Then
      Select Case (lData_Type)
        Case ("Short")
          lStrDataType = "Srt"
        Case ("Int16")
          Return "Int"
        Case ("Int32")
          Return "Int"
        Case ("Float")
          Return "Flt"
        Case ("Double")
          Return "Dbl"
        Case ("Long")
          Return "Lng"
        Case ("UShort")
          Return "Usrt"
        Case ("UInt32")
          Return "Uint"
        Case ("Double64")
          Return "Dbl"
        Case ("Byte")
          Return "Byt"
        Case ("Single")
          Return "Sng"
        Case ("String")
          Return "Str"
        Case ("Char")
          Return "Chr"
        Case ("User Defined DataType Array")
          Return "mArrObj"
        Case ("User Defined DataType")
          Return "mObj"
      End Select
    Else
      Select Case (lData_Type)
        Case ("Short")
          Return "ArrSrt"
        Case ("Int16")
          Return "ArrInt"
        Case ("Int32")
          Return "ArrInt"
        Case ("Float")
          Return "ArrFlt"
        Case ("Double")
          Return "ArrDbl"
        Case ("Long")
          Return "ArrLng"
        Case ("UShort")
          Return "ArrUsrt"
        Case ("UInt32")
          Return "ArrUint"
        Case ("Double64")
          Return "ArrDbl"
        Case ("Byte")
          Return "ArrByt"
        Case ("Single")
          Return "ArrSng"
        Case ("String")
          Return "ArrStr"
        Case ("Char")
          Return "ArrChr"
        Case ("User Defined DataType Array")
          Return "mArrObj"
        Case ("User Defined DataType")
          Return "mObj"
      End Select
    End If

    Return lStrDataType
  End Function




  Private Function fnGetPrptRtrnType(ByVal lData_Type As String) As String

    Select Case (lData_Type)
      Case ("Short")
        Return "Short"
      Case ("Int16")
        Return "Int16"
      Case ("Int32")
        Return "Int32"
      Case ("Float")
        Return "Float"
      Case ("Double")
        Return "Double"
      Case ("Long")
        Return "Long"
      Case ("UShort")
        Return "UShort"
      Case ("UInt32")
        Return "UInt32"
      Case ("Single")
        Return "Single"
      Case ("Byte")
        Return "Byte"
      Case ("Char")
        Return "Char"
      Case ("User Defined DataType Array")
        Return "mArrObj"
      Case ("User Defined DataType")
        Return "mObj"
      Case Else
        Return ""
    End Select
  End Function

  

End Class
